

#include "coord.h"
#include "iot_server.h"


#if defined(LOG_TAG)
#undef LOG_TAG 
#define LOG_TAG    "recv_coord_thread"
#else 
#define LOG_TAG    "recv_coord_thread"
#endif
char mob_str_buf[SIZE]; //发送监控数据字符串给移动端的缓冲区

void* recv_coord_thread(void *arg)
{
	int i;
//	int check = 0; //未同步
	char vbuf[SIZE];
	node_data buf;
	int ret = 0;
	int in_cnt = 0;
	int line=0;
	char byte; //用来保存串口一个字节的变量
	char syncok = 0; //是否同步成功的标志
	int fd = open("uart_data.dat", O_RDONLY);
	lseek(fd, 5, SEEK_SET);
	log_d("连接串口设备,正在同步...");
	while (1) {
		bzero(vbuf, SIZE);		
		
		//循环读取一个数据包
		while (1) {
			byte = 0;

			ret = read(fd, &byte, 1); //什么开始读串口是随机
			if (ret == 0) goto repeat;
			usleep(80000);
			//cnt++;
			if (byte == '#') {				
				byte = 0;
				ret = read(fd, &byte, 1);
				usleep(80000);
				if (ret == 0) goto repeat;
				if (byte == '#') {
					byte = 0;
					ret = read(fd, &byte, 1);
					usleep(80000);
					if (ret == 0) goto repeat;
				if (byte == 'I') {
					byte = 0;
					ret = read(fd, &byte, 1);
					usleep(80000);
					if (ret == 0) goto repeat;
					if (byte == 'D') {
						byte = 0;
						ret = read(fd, &byte, 1);
						usleep(80000);
						if (ret == 0) goto repeat;
						if (byte == 'T') {
							byte = 0;
							ret = read(fd, &byte, 1);
							usleep(80000);
							if (ret == 0) goto repeat;
							if (byte == 'H') {
								//##IDTH全部找到,说明正确的同步头开始
								//读取后面7个字节 一共13个字节为一个数据包
								for (i = 0; i < LEN_MONI; i++) {
									ret = read(fd, &byte, 1);
									if (ret == 0) goto repeat;
									buf.data[i] = byte;
									usleep(80000);
								}
								if (i == LEN_MONI) {
									syncok=1;
									line ++;
									
//									for (i = 0; i < LEN_MONI; i++) {
//										printf("%02x ", buf.data[i]);
//									}
									/*log_d("==%d== %02x %02x %02x %02x %02x %02x %02x", line, \
										buf.data[0], buf.data[1], buf.data[2], buf.data[3],\
										buf.data[4], buf.data[5], buf.data[6]);*/
									
								}
							}
						}
					}
				}
			}
		
		}
			
			if (syncok == 1) {

#if 0
				pthread_mutex_lock(&coord_mutex);//上锁
				bzero(send_mob_buf, SIZE);
				convert_moni_info(&buf, send_mob_buf);
				pthread_mutex_unlock(&coord_mutex);//释放锁
#endif
				bzero(mob_str_buf, SIZE);
				convert_moni_info(&buf, mob_str_buf);

				list_pnode new;
				
				//将一个终端节点数据保存到一个链表节点中
				new = (list_pnode) malloc(sizeof(list_node));
				if (NULL == new) {
					perror("malloc");
					return NULL;
				}
				memset(new, 0, sizeof(list_node)); //新节点清0
				memcpy(new->mob_str_buf, mob_str_buf, SIZE); 

				
				pthread_mutex_lock(&queue_mutex);
				linkqueue_in(pque, new);
				in_cnt++;
				#if 0
				log_i("入队节点编号:%d   %s\n",in_cnt, new->mob_str_buf		);
				#endif
				pthread_mutex_unlock(&queue_mutex);
				pthread_cond_signal(&queue_cond); //唤醒获取列队参数的线程
				
				syncok = 0;
			}
		}
		if (ret > 0) continue;
repeat:
		lseek(fd, 0, SEEK_SET);	
		line = 0;
	
	}

	return NULL;
	
}

